package com.lagou;/**
 * Created by IntelliJ IDEA.
 * User: Yangdx.
 * Date: 2020/12/14.
 * Time: 2:11.
 * Tags: Code, we are serious.
 */

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2020-12-14 2:11
 **/
@Component
public class RabbitMQConfig {

    //订单交换机
    public static String ORDER_EXCHANGE = "order.exchange";

    //订单队列
    public static String ORDER_QUEUE = "order.queue";

    //订单路由key
    public static String ORDER_ROUTING_KEY = "order.routing.key";

    //死信交换机
    public static String DLX_EXCHANGE = "dlx.exchange";

    //死信队列
    public static String DLX_QUEUE = "dlx.queue";

    //死信路由key
    public static String DLX_ROUTING_KEY = "dlx.routing.key";

    /**
     * 定义死信交换机
     */
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE);
    }

    /**
     * 定义死信队列
     */
    @Bean
    public Queue dlxQueue() {
        return new Queue(DLX_QUEUE);
    }

    /**
     * 死信交换器绑定死信队列
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(DLX_ROUTING_KEY);
    }

    /**
     * 定义订单业务交换机
     */
    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(ORDER_EXCHANGE);
    }

    /**
     * 定义订单队列
     */
    @Bean
    public Queue orderQueue() {
        Map<String, Object> props = new HashMap<>();
        // 绑定死信交换机
        props.put("x-dead-letter-exchange", DLX_EXCHANGE);
        // 绑定死信路由key
        props.put("x-dead-letter-routing-key", DLX_ROUTING_KEY);
        return new Queue(ORDER_QUEUE, true, false, false, props);
    }

    /**
     * 订单业务交换器绑定订单队列
     */
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_ROUTING_KEY);
    }
}

